package com.kcs.fxmargin.service;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import com.kcs.common.utils.ConnectionUtils;
import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.bo.DailyReportBO;
import com.kcs.fxmargin.dao.DealsSumDao;
import com.kcs.fxmargin.model.TDealsSum;

public class DealsSumService {
	DealsSumDao dao = new DealsSumDao();
	
	public TDealsSum search(String cifNo, String asOfDate, boolean cashFlag) throws Exception {
		
		Connection conn 		= null;
		PreparedStatement pstm 	= null;
		CallableStatement cstmt = null;
		ResultSet rs			= null;
		TDealsSum dealsSum		= null;
		
		try{
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
			
			
			if(cashFlag == true){ // is current Date
//				BigDecimal cashBalance 	= dao.getCashBalance(conn, pstm, rs, cifNo, asOfDate);
//				
//				if(dealsSum.getCashBal() != null && cashBalance != null)
//					dealsSum.setCashBal(dealsSum.getCashBal().add(cashBalance));
				dealsSum = dao.checkData(conn, pstm, rs);
				
				if(dealsSum == null) dao.callSPCallMarginLimit(conn, cstmt, cifNo, asOfDate);
			}
			
			
			dealsSum = dao.search(conn, pstm, rs, cifNo, asOfDate);
		} catch (Exception e){
			throw e;
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
			if(rs   != null ) rs.close();
		}
		
		return dealsSum;
	}
	
	public List<DailyReportBO> searchDailyReport(DailyReportBO bo) throws Exception {
		Connection conn 					= null;
		PreparedStatement pstm 				= null;
		ResultSet rs						= null;
		List<DailyReportBO> dailyReportList = null;
		try{
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO); // get connection fxmargin from mssql
			dailyReportList = dao.searchDailyReport(conn, pstm, rs, bo);
						
		} catch (Exception e){
			throw e;
		} finally {
			if(conn != null ) conn.close();
			if(pstm != null ) pstm.close();
			if(rs   != null ) rs.close();
		}
		
		return dailyReportList;
	}
}
